package ar.edu.itba.jMEBowling.scenario;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Cylinder;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.material.Material;

public class Gutter {

	private StaticPhysicsNode gutterNode;
	private final float COS45 = (float) (Math.sqrt(2) / 2);
	private final float cylRadius = 0.25f;

	public Gutter(float radius, int length, PhysicsSpace physicSpace,
			boolean right) {
		gutterNode = physicSpace.createStaticNode();
		Cylinder cyl1 = null;
		if (right) {
			cyl1 = createCylinder(new Vector3f(0, -cylRadius, 0), length,
					"cyl1");
		} else {
			cyl1 = createCylinder(new Vector3f(0, 0, 0), length, "cyl1");
		}

		Cylinder cyl2 = createCylinder(new Vector3f(radius - COS45 * radius,
				-COS45 * radius, 0), length, "cyl2");
		Cylinder cyl3 = createCylinder(new Vector3f(radius,
				-radius + cylRadius, 0), length, "cyl3");
		Cylinder cyl4 = createCylinder(new Vector3f(radius + COS45 * radius,
				-COS45 * radius, 0), length, "cyl4");
		Cylinder cyl5 = null;
		if (right) {
			cyl5 = createCylinder(new Vector3f(2 * radius, 0, 0), length,
					"cyl5");
		} else {
			cyl5 = createCylinder(new Vector3f(2 * radius, -cylRadius, 0),
					length, "cyl5");
		}

		gutterNode.attachChild(cyl1);
		gutterNode.attachChild(cyl2);
		gutterNode.attachChild(cyl3);
		gutterNode.attachChild(cyl4);
		gutterNode.attachChild(cyl5);
		gutterNode.setModelBound(new BoundingBox());
		gutterNode.updateModelBound();
		gutterNode.setMaterial(Material.IRON);
		gutterNode.generatePhysicsGeometry();

	}

	private Cylinder createCylinder(Vector3f pos, int length, String name) {
		final Cylinder cylinder = new Cylinder(name, 10, 10, cylRadius, length);

		cylinder.setLocalTranslation(pos);
		return cylinder;
	}

	public StaticPhysicsNode getGutterNode() {
		return this.gutterNode;
	}
}
